home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 October / Macworld (1998-10).dmg / Shareware World / Info / For Developers / MacZoop 1.8.4 / More Classes / Streaming Classes / ZClassRegistry.cpp next >
Encoding:
C/C++ Source or Header  |  1998-05-10  |  4.3 KB  |  207 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            MacZoop - "the framework for the rest of us"         
  5. *
  6. *
  7. *
  8. *            ZClassRegistry.cpp        -- registry of classes used for persistent objects
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1998, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21. #include    "ZClassRegistry.h"
  22. #include    "ZStream.h"
  23. #include    "ZDefines.h"
  24. #include    "ZErrors.h"
  25.  
  26. ZClassRegistry::ZClassRegistry()
  27.     : ZArray( sizeof( Object_Info ))
  28. {
  29.     classID = 'zreg';
  30. }
  31.  
  32.  
  33. ZClassRegistry::~ZClassRegistry()
  34. {
  35. }
  36.  
  37.  
  38. /*--------------------------------***  REGISTERCLASS  ***-------------------------------*/
  39. /*    
  40. register a class to allow it to be instantiated from stream
  41. ----------------------------------------------------------------------------------------*/
  42.  
  43. void        ZClassRegistry::RegisterClass( OSType aType, ConstructorFunction aFunc, Str255 aName )
  44. {
  45.     Object_Info        oi;
  46.     
  47.     if ( FindClass( aType ) == 0 )
  48.     {
  49.         oi.classid = aType;
  50.         oi.newfunc = aFunc;
  51.         CopyPStringTrunc( aName, oi.classname, 31 );
  52.         AppendItem( &oi );
  53.     }
  54. }
  55.  
  56.  
  57. /*-------------------------------***  GETNAMEOFCLASS  ***-------------------------------*/
  58. /*    
  59. return name of class given its ID.
  60. ----------------------------------------------------------------------------------------*/
  61.  
  62. void        ZClassRegistry::GetNameOfClass( OSType aType, Str31 aName )
  63. {
  64.     Object_Info        oi;
  65.     long            index = FindClass( aType );
  66.     
  67.     if ( index > 0 )
  68.     {
  69.         GetArrayItem( &oi, index );
  70.         CopyPString( oi.classname, aName );
  71.     }
  72.     else
  73.         FailOSErr( kClassTypeUnknownErr );
  74. }
  75.  
  76.  
  77. /*------------------------------***  INSTANTIATECLASS  ***------------------------------*/
  78. /*    
  79. create a new object with the given class ID.
  80. ----------------------------------------------------------------------------------------*/
  81.  
  82. ZObject*    ZClassRegistry::InstantiateClass( OSType aType )
  83. {
  84.     ZObject*        obj = NULL;
  85.     Object_Info        oi;
  86.     long            index;
  87.     
  88.     index = FindClass( aType );
  89.     
  90.     if ( index > 0 )
  91.     {
  92.         GetArrayItem( &oi, index );
  93.         
  94.         if ( oi.newfunc == NULL )
  95.             FailOSErr( kBadConstructorFuncErr );
  96.         
  97.         obj = (*oi.newfunc)();
  98.     }
  99.     else
  100.         FailOSErr( kClassTypeUnknownErr );
  101.     
  102.     return obj;
  103. }
  104.  
  105.  
  106. /*------------------------------***  INSTANTIATECLASS  ***------------------------------*/
  107. /*    
  108. create a new object with the given class name.
  109. ----------------------------------------------------------------------------------------*/
  110.  
  111. ZObject*    ZClassRegistry::InstantiateClass( Str31 aName )
  112. {
  113.     ZObject*        obj = NULL;
  114.     Object_Info        oi;
  115.     long            index;
  116.     
  117.     index = FindClass( aName );
  118.     
  119.     if ( index > 0 )
  120.     {
  121.         GetArrayItem( &oi, index );
  122.         
  123.         if ( oi.newfunc == NULL )
  124.             FailOSErr( kBadConstructorFuncErr );
  125.         
  126.         obj = (*oi.newfunc)();
  127.     }
  128.     else
  129.         FailOSErr( kClassNameUnknownErr );
  130.     
  131.     return obj;
  132. }
  133.  
  134.  
  135. /*---------------------------------***  FINDCLASS  ***----------------------------------*/
  136. /*    
  137. return index of class with given ID in registry (internal method)
  138. ----------------------------------------------------------------------------------------*/
  139.  
  140. long        ZClassRegistry::FindClass( OSType aType )
  141. {
  142.     Object_Info        oi;
  143.     long            i;
  144.     
  145.     for( i = 1; i <= CountItems(); i++ )
  146.     {
  147.         GetArrayItem( &oi, i );
  148.         
  149.         if ( oi.classid == aType )
  150.             return i;
  151.     }
  152.     
  153.     return 0;
  154. }
  155.  
  156.  
  157. /*---------------------------------***  FINDCLASS  ***----------------------------------*/
  158.  
  159. long        ZClassRegistry::FindClass( Str31 aName )
  160. {
  161.     Object_Info        oi;
  162.     long            i;
  163.     
  164.     for( i = 1; i <= CountItems(); i++ )
  165.     {
  166.         GetArrayItem( &oi, i );
  167.         
  168.         if ( EqualString( oi.classname, aName, FALSE, TRUE ))
  169.             return i;
  170.     }
  171.     
  172.     return 0;
  173. }
  174.  
  175.  
  176. #pragma mark -
  177.  
  178.  
  179. /*---------------------------***  INSTANTIATEFROMSTREAM  ***----------------------------*/
  180. /*    
  181. create a new object from the stream- normally this is called as necessary from the stream
  182. itself when an object is encountered.
  183. ----------------------------------------------------------------------------------------*/
  184.  
  185. ZObject*    ZClassRegistry::InstantiateFromStream( ZStream* aStream )        
  186. {
  187.     ZObject*    root = NULL;
  188.     OSType        rootClassID;
  189.  
  190.     FailNILParam( aStream );
  191.     
  192.     // class ID must be next data in stream
  193.     
  194.     aStream->ReadLong((long*) &rootClassID );
  195.     aStream->Skip( sizeof( long ));    // ignore instance ID stored in stream
  196.     
  197.     // instantiate this class:
  198.     
  199.     root = InstantiateClass( rootClassID );
  200.     
  201.     // read the rest of the object's data (and other objects if necessary) into object
  202.     
  203.     root->ReadFromStream( aStream );
  204.     
  205.     return root;
  206. }
  207.